<?php

namespace App\Models\Back;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Intervention\Image\ImageManager;

class Setting extends Model
{
	/*
	 * 更新
	 * @param   $request    array   表单数据数组
	 * return   boot    数据库更新成功返回true否则返回false
	 */
    public function set($request)
    {
	    unset($request['_token']);
	    //使用事务，循环插入
	    return DB::transaction(function () use($request) {
	    	foreach ($request as $key => $option) {
	    		$table = $key.'s';
			    foreach ($option as $k => $value) {
				    if (is_array($value)) {
					    $v = implode(",", $value);
				    } else {
					    $v = $value;
				    }
				    DB::table($table)
					    ->where('id', $k)
					    ->update(['value' => $v]);
			    }
		    }
	    });
    }

    //更新单个设置值
	public function setOne($request)
	{
		unset($request['_token']);
		$id = $request['setting_id'];
		if (is_array($request['value'])) {
			$value = implode(",", $request['value']);
		} else {
			$value = $request['value'];
		}
		return DB::table('settings')
				->where('id', $id)
				->update(['value' => $value]);

	}

    /*
     * 添加或修改表单验证
     * @param   array   $request    表单数据数组
     * return   mix     验证通过自动返回true，验证失败自动重定向到上一个页面
     */
	public function setValidator($request)
	{

		Validator::make($request, [
			'setting' => [
				'required',
				'array',
			],
			'setting.*' => [
				'required',
			]
		])->validate();
	}

	/*
     * 添加或修改表单验证单项
     * @param   array   $request    表单数据数组
     * return   mix     验证通过自动返回true，验证失败自动重定向到上一个页面
     */
	public function setOneValidator($request)
	{

		Validator::make($request, [
			'value' => [
				'required',
			]
		])->validate();
	}

	/*
     * 删除表单验证
     * @param   array   $request    表单数据数组
     * return   mix     验证通过自动返回true，验证失败自动重定向到上一个页面
     */
	public function deleteValidator($request)
	{
		Validator::make($request, [
			'selected' => [
				'required',
				'array'
			],
			'selected.*' => [
				'integer'
			]
		])->validate();
	}

	/*
	 * 删除品牌，支持批量删除
	 * @param   array   $idList 要删除的品牌id列表
	 */
	public function deleteSetting($idList)
	{
		//删除数据库信息
		return self::destroy($idList);
	}

	/*
	 * 品牌排序问题
	 */
	public function settingOrder($field, $sort)
	{
		$sort = $sort == 'asc' ? $sort : 'desc';
		return self::orderBy($field, $sort)->simplePaginate(config('tool.back.page', 5));
	}

	/*
	 * 获取配置分组
	 */
	public function getSettingGroup()
	{
		$settingGroup = DB::table('setting_groups')->select('id', 'title', 'created_at')->get();
		return $settingGroup;
	}

	/*
	 * 获取配置选项
	 */
	public function getSettingOption()
	{
		$settingOptionList = DB::table('setting_options')->select('id', 'title', 'setting_id', 'created_at')->get();
		return $settingOptionList;
	}

	/*
	 * 获取配置
	 */
	public function getSetting()
	{
		$settingList = self::join('setting_types', 'settings.setting_type_id', '=', 'setting_types.id')
				->select('settings.*', 'setting_types.title as type_title')
				->get()->toArray();

		//对象转数组
		$settingOptionList = json_decode($this->getSettingOption(), true);

		//整合
		foreach ($settingList as $key => $setting) {
			foreach ($settingOptionList as $settingOption) {
				if ($setting['id'] == $settingOption['setting_id']) {
					$settingList[$key]['optionList'][] = $settingOption;
				}
			}
		}

		return $settingList;
	}
}
